##############################################################################
# Replication Code for 
# Book "The Credibility Challenge: How Democracy Aid Influences Election Violence"
# Inken von Borzyskowski 
# Cornell University Press 2019  
#
# Last Updated: June 2019
##############################################################################
  
#Set Working Directory
#setwd("")




##########################
# Figure I.3  
##########################
library(cshapes)
library(foreign)
library(classInt)
library(RColorBrewer)
dat<-read.table("UNmap.csv", sep=",", header=TRUE, na=NA)
attach(dat)

dat$IB[dat$AvgUNTA == NA] <- 0
dat$IB[dat$AvgUNTA == 0.0 ] <- 1
dat$IB[dat$AvgUNTA > 0.01 & dat$AvgUNTA <= 0.194] <- 2
dat$IB[dat$AvgUNTA > 0.195 & dat$AvgUNTA <= 0.394] <- 3
dat$IB[dat$AvgUNTA > 0.395 & dat$AvgUNTA <= 0.594] <- 4
dat$IB[dat$AvgUNTA > 0.595] <- 5
IB=as.numeric(dat$IB)
attach(dat)

# Extract Countries Boundaries 1.1.2010
cshp.data <- cshp()
summary(cshp.data)
cshp.data2010 <- cshp(date=as.Date("2010-1-1"))
world<- cshp.data2010[cshp.data2010$COWCODE>=2,]

# Combine with the UN data
o<-match(world$COWCODE,dat$cowcode)
dat<-dat[o,]
row.names(dat)<-world$FEATUREID
world.merged<-spCbind(world,dat)

# Create Map
dev.off()
pal <- brewer.pal(5, "Greys")
breaks4 <- classIntervals(world.merged$AvgUNTA, n=5, style="fixed", fixedBreaks=c(0,0.001,0.194,0.394,0.594,1))
colors4 <- findColours(breaks4, pal)
par(mar=c(4.1,0.1,4.1,2.1))             #sets the bottom, left, top and right margins
plot(world.merged, bty="n", col=colors4)
legend(x=-250, y=32,legend=c("0%", "1-19%","20-39%","40-59%","60-100%"),fill = attr(colors4, "palette"),bty = "n", title = "UN Technical Election Assistance", cex=0.6, title.adj = 0.38, y.intersp=0.75)
# save as 7 by 14 landscape


dev.off()
detach(dat)






##########################
# Figure 2.1  
##########################

library(haven)
dat <- read_dta("Book_data.dta")
#install.packages('plotrix')
library(plotrix)

dat <- dat[which(dat$PreDay_killedInjured>=1),]

I <- 100
histo <- matrix(NA,1,I)
for (i in 1 : I) {
  
  histo[1,i]   <- nrow(dat[which(dat$PreDay_killedInjured<=i*10 & dat$PreDay_killedInjured>(i-1)*10),])
  
}
black <- matrix("Dark Grey",1,I)

gap.barplot(histo,gap=c(12,59),xlab="Pre-election casualties",cex.lab=1.5 ,ytics=c(0,5,10,15,60,65), ylim=c(0,19), ylab="Frequency", col = black, xaxt = "n",  yaxs="i")
axis(1, at=c(0,10,20,30,40,50,60,70,80,90,100), labels=c("0","","200","","400","","600","","800","","1000"), cex=1.5)
# save as 5 by 12 landscape

dev.off()




##########################
# Figure 4.1 
##########################

# Load packages
library("ggplot2")
theme_set(theme_bw())
library("scales")
library("gridExtra")
library("reshape2")

# Load data 
ds<-read.table("Figure_4_1.csv", sep=",", header=TRUE)
attach(ds)

# Create min and max
min95<-coef-(1.96*se)
max95<-coef+(1.96*se)
dst<-cbind(ds,min95,max95)

dst$model <- factor(dst$model, levels=c( "Africa only, full predictors", "Africa only, basic predictors", "LAC and Africa, full predictors", "LAC and Africa, basic predictors"))
dst$outcome <- factor(dst$outcome, levels=c("Election Commission Capacity", "Clean Voting","Election Commission Independence", "Loser Challenge",  "Clean Election Index"))

plotA<-ggplot(dst) +
  geom_pointrange(aes(ymin=min95, ymax=max95, x=model,y=coef, color=as.factor(ErrorSignificant), shape=as.factor(SingleStage)), size=1,  position=position_dodge(width=0.4)) + #defining points and CI
  coord_flip() + # rotate graph 90 degrees
  facet_wrap(~ outcome, ncol = 2) + #Facetting
  geom_hline(aes(yintercept=0), linetype="dashed", size=1) + #add line at zero
  xlab("") + #no label
  ylab("") + # no label y axis
  theme(axis.text.x=element_text(colour="#000000", size=16)) + #makes scale on x axis black and size
  theme(axis.text.y=element_text(colour="#000000", size=16)) + #makes scale on y axis black and size
  scale_color_grey(start = 0.7, end = 0.1, name = "Two-Stage Error Correlation", labels=c("not significant", "significant")) +
  scale_shape_manual(name = "Single Stage Estimates", labels = c("no", "yes"), values = c(16, 1)) + 
  theme(axis.title.x=element_text(size=18, angle=90, vjust=0.3)) +  #placing the y axis label  and adjusting letter size
  theme_bw() + #makes background white and black borders
  theme(legend.position="bottom")

ggsave(plotA, file="Figure_4_1.pdf", width=10, height=8, dpi = 2400)